#!/bin/bash
set -euo pipefail

HERE="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(cd "$HERE/.." && pwd)"

SERVICE_NAME="cat-photo-ui.service"
SERVICE_SRC="$ROOT/systemd/$SERVICE_NAME"
SERVICE_DST="/etc/systemd/system/$SERVICE_NAME"

if [[ $EUID -ne 0 ]]; then
  echo "请使用sudo运行本脚本" >&2
  exit 1
fi

# 安装服务单元
install -Dm644 "$SERVICE_SRC" "$SERVICE_DST"

# 安装配置目录与默认配置（若不存在）
install -d -m 755 /etc/cat_photo_ui
if [[ ! -f /etc/cat_photo_ui/config.toml ]]; then
  if [[ -f "$ROOT/config.toml" ]]; then
    install -Dm644 "$ROOT/config.toml" /etc/cat_photo_ui/config.toml
  else
    install -Dm644 "$ROOT/config.example.toml" /etc/cat_photo_ui/config.toml
  fi
fi

systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"

echo "已安装并启动：$SERVICE_NAME"


